package com.cms.model;

import java.util.List;

import com.cms.common.kit.Constant;
import com.cms.common.kit.Tree;
import com.cms.common.kit.TreeBuilder;
import com.cms.plugin.db.Model;
import com.cms.plugin.db.Page;
import com.cms.plugin.db.annotation.Table;



@Table("s_res")
public class Res extends Model<Res>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6183022815040778078L;
	public static final Res dao = new Res();
	
	/**
	 * 所有 sql 写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<Res> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", "from s_res order by seq");
	}
	
	
	private  List<Res> children;

	public List<Res> getChildren() {
		return children;
	}

	public void setChildren(List<Res> children) {
		this.children = children;
	}

	public String getResByRoleId(String roleId) {
		List<Res> find = find("select res_id from s_res_role where role_id=?", roleId);
		String res_ids="";
		for (Res res : find) {
			res_ids+=res.getInt("res_id")+",";
		}
		if(res_ids.length()>1){
			res_ids=res_ids.substring(0,res_ids.length()-1);
		}
		return res_ids;
	}

	public List<Res> findAllRes() {
		return findByCache(Constant.CACHE_GLOBAL, Constant.CACHE_GLOBAL_PERMISSIONS, "select * from s_res");
	}

	public List<Res> findByAdminId(String admin_id) {
		String sql="select * from s_res where id in(select res_id from s_res_role where role_id=(select role_id from s_role_admin where admin_id=?))";
		return findByCache(Constant.CACHE_SYSTEM, Constant.USER_PERMISSION_KEY+"_"+admin_id, sql, admin_id);
	}
	
	
}
